*     *****************************************************************   *;
*     *****************************************************************   *; 
*   Date:          December 27, 2021                               	      *;
*   Authors:       Lisa Dellmuth and Jonas Tallberg				          *;   
*   Purpose: 	   Analyses for book Legitimacy Politics: Elite  		  *;
*    Communication and Public Opinion in Global Governance (CUP, 2023)    *;
*     ****************************************************************    *;
*     ****************************************************************    *;


*round 2 : 2002, 2003, 2004

use merged_r2_data.dta, clear

tab dateintr
tab country

gen year=year(dateintr)
br country dateintr year q53gnew q53anew

rename q53gnew  sadc_ecowas_trust   
rename q53anew  au_trust

drop if year == 1582

tab year
tab country


tab au_trust
tab au_trust, nol
tab sadc_ecowas_trust, nol

 replace au_trust =. if au_trust== -1 
 replace au_trust =. if au_trust== 99 

 replace sadc_ecowas_trust =. if sadc_ecowas_trust== -1 
 replace sadc_ecowas_trust =. if sadc_ecowas_trust== 99 

 
 tab country
 
decode country, gen(country_str)
tab country_str
		 
sort country_str		 
encode country_str, gen(country_n)		 

tab country_str


replace	country_n	=12 	if	country_str	==	"Algeria"
replace	country_n	=204	if	country_str	==	"Benin"
replace	country_n	=72	    if	country_str	==	"Botswana"
replace	country_n	=854	if	country_str	==	"Burkina Faso"
replace	country_n	=108	if	country_str	==	"Burundi"
replace	country_n	=120	if	country_str	==	"Cameroon"
replace	country_n	=132    if	country_str	==	"Cape Verde"
replace	country_n	=818	if	country_str	==	"Egypt"
replace	country_n	=9991   if	country_str	==	"Gabon"
replace	country_n	=288	if	country_str	==	"Ghana"
replace	country_n	=324	if	country_str	==	"Guinea"
replace	country_n	=404	if	country_str	==	"Kenya"
replace	country_n	=426	if	country_str	==	"Lesotho"
replace	country_n	=430	if	country_str	==	"Liberia"
replace	country_n	=450	if	country_str	==	"Madagascar"
replace	country_n	=454	if	country_str	==	"Malawi"
replace	country_n	=466	if	country_str	==	"Mali"
replace	country_n	=480    if	country_str	==	"Mauritius"
replace	country_n	=504	if	country_str	==	"Morocco"
replace	country_n	=508	if	country_str	==	"Mozambique"
replace	country_n	=516	if	country_str	==	"Namibia"
replace	country_n	=562	if	country_str	==	"Niger"
replace	country_n	=566	if	country_str	==	"Nigeria"
replace	country_n	=9992	if	country_str	==	"São Tomé and Príncipe"
replace	country_n	=686	if	country_str	==	"Senegal"
replace	country_n	=694	if	country_str	==	"Sierra Leone"
replace	country_n	=710	if	country_str	==	"South Africa"
replace	country_n	=736	if	country_str	==	"Sudan"
replace	country_n	=748	if	country_str	==	"Swaziland"
replace	country_n	=834	if	country_str	==	"Tanzania"
replace	country_n	=768	if	country_str	==	"Togo"
replace	country_n	=788	if	country_str	==	"Tunisia"
replace	country_n	=800	if	country_str	==	"Uganda"
replace	country_n	=894	if	country_str	==	"Zambia"
replace	country_n	=716	if	country_str	==	"Zimbabwe"

rename country country_afrobarometer
rename country_n country
rename country_str country_name 

label define name 12 "Algeria" 204 "Benin" 72 "Botswana" 854 "Burkina Faso" 108 "Burundi" 120 "Cameroon" 132 "Cape Verde" /*
*/  384	"Cote d'Ivoire" 818	"Egypt" 9991 "Gabon" 288 "Ghana" 324 "Guinea" 404 "Kenya" 426 "Lesotho" /*
*/ 430 "Liberia" 450 "Madagascar" 454 "Malawi" 466 "Mali" 480 "Mauritius" 504	"Morocco" 508 "Mozambique" /*
*/ 516 "Namibia" 562 "Niger" 566 "Nigeria" 9992	"São Tomé and Príncipe" 686	"Senegal" 694 "Sierra Leone" /*
*/ 710 "South Africa" 736 "Sudan" 748 "Swaziland" 834 "Tanzania" 768 "Togo" 788 "Tunisia" 800 "Uganda" /*
*/ 894 "Zambia" 716 "Zimbabwe"


 la val country name
 
 


label var country "country codes of World Values Survey"
label var country_afrobarometer "country codes of Afrobarometer"

order country country_name country_afrobarometer year


sum withinwt acrosswt combinwt //country weights
keep country country_name /*country_afrobarometer*/ year au_trust sadc_ecowas_trust withinwt acrosswt combinwt

save Afrobarometer_r2.dta, replace




/*
*round 3
use merged_r3_data.dta, clear

tab dateintr
tab country

gen year=year(dateintr)
br country dateintr year q53gnew q53anew

rename q53gnew au_trust
rename q53anew sadc_ecowas_trust 


keep country year au_trust sadc_ecowas_trust

*save Afrobarometer_r2.dta, replace
*/



*round 4: years 2008,2009
use merged_r4_data.dta, clear

tab DATEINTR
tab COUNTRY

gen year=year(DATEINTR)

rename COUNTRY country 

tab Q98A
tab Q98B

rename Q98A au_trust
rename Q98B ecowas_trust

 br country DATEINTR year ecowas_trust au_trust
  
 tab au_trust
 
tab year
tab country


tab au_trust
tab au_trust, nol
tab ecowas_trust
tab ecowas_trust, nol

 replace au_trust =. if au_trust== -1 
 replace au_trust =. if au_trust== 998 
 replace au_trust =. if au_trust== 9

 replace ecowas_trust =. if ecowas_trust== -1 
 replace ecowas_trust =. if ecowas_trust== 998
 replace ecowas_trust =. if ecowas_trust== 9

 
 decode country, gen(country_str)
 tab country_str
		 
sort country_str		 
encode country_str, gen(country_n)		 


tab country_str

replace	country_n	=12 	if	country_str	==	"Algeria"
replace	country_n	=204	if	country_str	==	"Benin"
replace	country_n	=72	    if	country_str	==	"Botswana"
replace	country_n	=854	if	country_str	==	"Burkina Faso"
replace	country_n	=108	if	country_str	==	"Burundi"
replace	country_n	=120	if	country_str	==	"Cameroon"
replace	country_n	=132    if	country_str	==	"Cape Verde"
replace	country_n	=818	if	country_str	==	"Egypt"
replace	country_n	=9991   if	country_str	==	"Gabon"
replace	country_n	=288	if	country_str	==	"Ghana"
replace	country_n	=324	if	country_str	==	"Guinea"
replace	country_n	=404	if	country_str	==	"Kenya"
replace	country_n	=426	if	country_str	==	"Lesotho"
replace	country_n	=430	if	country_str	==	"Liberia"
replace	country_n	=450	if	country_str	==	"Madagascar"
replace	country_n	=454	if	country_str	==	"Malawi"
replace	country_n	=466	if	country_str	==	"Mali"
replace	country_n	=480    if	country_str	==	"Mauritius"
replace	country_n	=504	if	country_str	==	"Morocco"
replace	country_n	=508	if	country_str	==	"Mozambique"
replace	country_n	=516	if	country_str	==	"Namibia"
replace	country_n	=562	if	country_str	==	"Niger"
replace	country_n	=566	if	country_str	==	"Nigeria"
replace	country_n	=9992	if	country_str	==	"São Tomé and Príncipe"
replace	country_n	=686	if	country_str	==	"Senegal"
replace	country_n	=694	if	country_str	==	"Sierra Leone"
replace	country_n	=710	if	country_str	==	"South Africa"
replace	country_n	=736	if	country_str	==	"Sudan"
replace	country_n	=748	if	country_str	==	"Swaziland"
replace	country_n	=834	if	country_str	==	"Tanzania"
replace	country_n	=768	if	country_str	==	"Togo"
replace	country_n	=788	if	country_str	==	"Tunisia"
replace	country_n	=800	if	country_str	==	"Uganda"
replace	country_n	=894	if	country_str	==	"Zambia"
replace	country_n	=716	if	country_str	==	"Zimbabwe"

rename country country_afrobarometer
rename country_n country
rename country_str country_name 

label define name 12 "Algeria" 204 "Benin" 72 "Botswana" 854 "Burkina Faso" 108 "Burundi" 120 "Cameroon" 132 "Cape Verde" /*
*/  384	"Cote d'Ivoire" 818	"Egypt" 9991 "Gabon" 288 "Ghana" 324 "Guinea" 404 "Kenya" 426 "Lesotho" /*
*/ 430 "Liberia" 450 "Madagascar" 454 "Malawi" 466 "Mali" 480 "Mauritius" 504	"Morocco" 508 "Mozambique" /*
*/ 516 "Namibia" 562 "Niger" 566 "Nigeria" 9992	"São Tomé and Príncipe" 686	"Senegal" 694 "Sierra Leone" /*
*/ 710 "South Africa" 736 "Sudan" 748 "Swaziland" 834 "Tanzania" 768 "Togo" 788 "Tunisia" 800 "Uganda" /*
*/ 894 "Zambia" 716 "Zimbabwe"


 la val country name
 
 
 
 tab country_afrobarometer
 tab country_afrobarometer, nol
 


label var country "country codes of World Values Survey"
label var country_afrobarometer "country codes of Afrobarometer"

order country country_name country_afrobarometer year

sum Withinwt Acrosswt Combinwt //country wieights
rename Withinwt withinwt
rename Acrosswt acrosswt 
rename Combinwt combinwt

keep country country_name /*country_afrobarometer*/ year ecowas_trust au_trust withinwt acrosswt combinwt

 
save Afrobarometer_r4.dta, replace

 
 

*round 5: year 2011, 2012, 2013
use merged_r5_data.dta, clear


sum COUNTRY DATEINTR
tab DATEINTR

tab Q78A1 

gen year=year(DATEINTR)

rename COUNTRY country 
rename Q78A1 au_trust 

tab year
tab country

tab au_trust
tab au_trust, nol

 replace au_trust =. if au_trust== -1 
 replace au_trust =. if au_trust== 997 
 replace au_trust =. if au_trust== 9
 
 
  decode country, gen(country_str)
 tab country_str
		 
sort country_str		 
encode country_str, gen(country_n)		 

tab country_str
tab country_str, nol

tab country
tab country, nol


replace	country_n	=12 	if	country_str	==	"Algeria"
replace	country_n	=204	if	country_str	==	"Benin"
replace	country_n	=72	    if	country_str	==	"Botswana"
replace	country_n	=854	if	country_str	==	"Burkina Faso"
replace	country_n	=108	if	country_str	==	"Burundi"
replace	country_n	=120	if	country_str	==	"Cameroon"
replace	country_n	=132    if	country_str	==	"Cape Verde"
replace	country_n	=384	if	country	==	27
replace	country_n	=818	if	country_str	==	"Egypt"
replace	country_n	=9991   if	country_str	==	"Gabon"
replace	country_n	=288	if	country_str	==	"Ghana"
replace	country_n	=324	if	country_str	==	"Guinea"
replace	country_n	=404	if	country_str	==	"Kenya"
replace	country_n	=426	if	country_str	==	"Lesotho"
replace	country_n	=430	if	country_str	==	"Liberia"
replace	country_n	=450	if	country_str	==	"Madagascar"
replace	country_n	=454	if	country_str	==	"Malawi"
replace	country_n	=466	if	country_str	==	"Mali"
replace	country_n	=480    if	country_str	==	"Mauritius"
replace	country_n	=504	if	country_str	==	"Morocco"
replace	country_n	=508	if	country_str	==	"Mozambique"
replace	country_n	=516	if	country_str	==	"Namibia"
replace	country_n	=562	if	country_str	==	"Niger"
replace	country_n	=566	if	country_str	==	"Nigeria"
replace	country_n	=9992	if	country_str	==	"São Tomé and Príncipe"
replace	country_n	=686	if	country_str	==	"Senegal"
replace	country_n	=694	if	country_str	==	"Sierra Leone"
replace	country_n	=710	if	country_str	==	"South Africa"
replace	country_n	=736	if	country_str	==	"Sudan"
replace	country_n	=748	if	country_str	==	"Swaziland"
replace	country_n	=834	if	country_str	==	"Tanzania"
replace	country_n	=768	if	country_str	==	"Togo"
replace	country_n	=788	if	country_str	==	"Tunisia"
replace	country_n	=800	if	country_str	==	"Uganda"
replace	country_n	=894	if	country_str	==	"Zambia"
replace	country_n	=716	if	country_str	==	"Zimbabwe"

rename country country_afrobarometer
rename country_n country
rename country_str country_name 

label define name 12 "Algeria" 204 "Benin" 72 "Botswana" 854 "Burkina Faso" 108 "Burundi" 120 "Cameroon" 132 "Cape Verde" /*
*/  384	"Cote d'Ivoire" 818	"Egypt" 9991 "Gabon" 288 "Ghana" 324 "Guinea" 404 "Kenya" 426 "Lesotho" /*
*/ 430 "Liberia" 450 "Madagascar" 454 "Malawi" 466 "Mali" 480 "Mauritius" 504	"Morocco" 508 "Mozambique" /*
*/ 516 "Namibia" 562 "Niger" 566 "Nigeria" 9992	"São Tomé and Príncipe" 686	"Senegal" 694 "Sierra Leone" /*
*/ 710 "South Africa" 736 "Sudan" 748 "Swaziland" 834 "Tanzania" 768 "Togo" 788 "Tunisia" 800 "Uganda" /*
*/ 894 "Zambia" 716 "Zimbabwe"


 la val country name


label var country "country codes of World Values Survey"
label var country_afrobarometer "country codes of Afrobarometer"

order country country_name country_afrobarometer year

sum withinwt combinwt // country weights

keep country country_name /*country_afrobarometer*/ year  au_trust withinwt combinwt



save Afrobarometer_r5.dta, replace





*round 6: year 2014, 2015

use merged_r6_data.dta, clear

sum COUNTRY DATEINTR Q79A Q79B
tab DATEINTR

tab Q79A  
tab Q79B 

gen year=year(DATEINTR)

rename COUNTRY country 
rename Q79A sadc_ecowas_trust
rename Q79B au_trust

tab year
tab country


tab au_trust
tab au_trust, nol 
tab sadc_ecowas_trust
tab sadc_ecowas_trust, nol

 replace au_trust =. if au_trust== -1 
 replace au_trust =. if au_trust== 9 

 replace sadc_ecowas_trust =. if sadc_ecowas_trust== -1 
 replace sadc_ecowas_trust =. if sadc_ecowas_trust== 9 
 replace sadc_ecowas_trust =. if sadc_ecowas_trust== 99 


  decode country, gen(country_str)
 tab country_str
		 
sort country_str		 
encode country_str, gen(country_n)		 


tab country_str

tab country
tab country, nol

replace	country_n	=12 	if	country_str	==	"Algeria"
replace	country_n	=204	if	country_str	==	"Benin"
replace	country_n	=72	    if	country_str	==	"Botswana"
replace	country_n	=854	if	country_str	==	"Burkina Faso"
replace	country_n	=108	if	country_str	==	"Burundi"
replace	country_n	=120	if	country_str	==	"Cameroon"
replace	country_n	=132    if	country_str	==	"Cape Verde"
replace	country_n	=384	if	country	==	8
replace	country_n	=818	if	country_str	==	"Egypt"
replace	country_n	=9991   if	country_str	==	"Gabon"
replace	country_n	=288	if	country_str	==	"Ghana"
replace	country_n	=324	if	country_str	==	"Guinea"
replace	country_n	=404	if	country_str	==	"Kenya"
replace	country_n	=426	if	country_str	==	"Lesotho"
replace	country_n	=430	if	country_str	==	"Liberia"
replace	country_n	=450	if	country_str	==	"Madagascar"
replace	country_n	=454	if	country_str	==	"Malawi"
replace	country_n	=466	if	country_str	==	"Mali"
replace	country_n	=480   if	country_str	==	"Mauritius"
replace	country_n	=504	if	country_str	==	"Morocco"
replace	country_n	=508	if	country_str	==	"Mozambique"
replace	country_n	=516	if	country_str	==	"Namibia"
replace	country_n	=562	if	country_str	==	"Niger"
replace	country_n	=566	if	country_str	==	"Nigeria"
replace	country_n	=9992	if	country_str	==	"São Tomé and Príncipe"
replace	country_n	=686	if	country_str	==	"Senegal"
replace	country_n	=694	if	country_str	==	"Sierra Leone"
replace	country_n	=710	if	country_str	==	"South Africa"
replace	country_n	=736	if	country_str	==	"Sudan"
replace	country_n	=748	if	country_str	==	"Swaziland"
replace	country_n	=834	if	country_str	==	"Tanzania"
replace	country_n	=768	if	country_str	==	"Togo"
replace	country_n	=788	if	country_str	==	"Tunisia"
replace	country_n	=800	if	country_str	==	"Uganda"
replace	country_n	=894	if	country_str	==	"Zambia"
replace	country_n	=716	if	country_str	==	"Zimbabwe"

rename country country_afrobarometer
rename country_n country
rename country_str country_name 

label define name 12 "Algeria" 204 "Benin" 72 "Botswana" 854 "Burkina Faso" 108 "Burundi" 120 "Cameroon" 132 "Cape Verde" /*
*/  384	"Cote d'Ivoire" 818	"Egypt" 9991 "Gabon" 288 "Ghana" 324 "Guinea" 404 "Kenya" 426 "Lesotho" /*
*/ 430 "Liberia" 450 "Madagascar" 454 "Malawi" 466 "Mali" 480 "Mauritius" 504	"Morocco" 508 "Mozambique" /*
*/ 516 "Namibia" 562 "Niger" 566 "Nigeria" 9992	"São Tomé and Príncipe" 686	"Senegal" 694 "Sierra Leone" /*
*/ 710 "South Africa" 736 "Sudan" 748 "Swaziland" 834 "Tanzania" 768 "Togo" 788 "Tunisia" 800 "Uganda" /*
*/ 894 "Zambia" 716 "Zimbabwe"

 la val country name
 
 tab country
tab country, nol



label var country "country codes of World Values Survey"
label var country_afrobarometer "country codes of Afrobarometer"


sum withinwt Combinwt //country weights

rename Combinwt combinwt

order country country_name country_afrobarometer year
keep country country_name /*country_afrobarometer*/ year sadc_ecowas_trust au_trust withinwt combinwt


save Afrobarometer_r6.dta, replace



*round 7: year 2016 2017 2018

/*
use merged_r7_data.dta, clear

sum COUNTRY DATEINTR 
tab DATEINTR

gen year=year(DATEINTR)

rename COUNTRY country 
rename Q79A sadc_ecowas_trust
rename Q79B au_trust

tab year
tab country

keep country year sadc_ecowas_trust au_trust

save Afrobarometer_r7.dta, replace

*/


************



 *Append all datasets: to create one dataset with individual observations of AU SADC and ECOWAS for each country and year
 
 use Afrobarometer_r2.dta, clear

 tab year 
 tab country

 count
 
 *la val country name

 
 append using Afrobarometer_r4.dta  // something wrong with country
 
 count
 tab year
 tab country
 tab country, nol

 order  year country

  la val country name


 
 append using Afrobarometer_r5.dta
 
  la val country name

 append using Afrobarometer_r6.dta

   la val country name

save Afrobarometer_final.dta, replace


***************


count

 tab year
 tab country


 use Afrobarometer_r2.dta, clear
tab country 

 use Afrobarometer_r4.dta, clear
tab country

 use Afrobarometer_r5.dta, clear
 tab country
 
 use Afrobarometer_r6.dta, clear
tab country

 
 
  use Afrobarometer_final.dta, clear

  tab country
 tab country, nol

 
